#include "ui_log.h"
#include <fstream>
#include <time.h>


namespace UI
{

UI_Log::t_LogList UI_Log::GetLogEntries(unsigned int iSeverity) const
{
	t_LogList OutLog;
	for (t_LogList::const_iterator iter = m_LogList.begin(); iter != m_LogList.end(); ++iter)
	{
		if (iSeverity == 0 || iter->m_iSeverity == iSeverity)
		{
			OutLog.push_back(*iter);
		}
	}
	return OutLog;
}


UI_Log::t_LogList UI_Log::GetLogEntries(unsigned int iHighSeverity, unsigned int iLowSeverity) const
{
	t_LogList OutLog;
	for (t_LogList::const_iterator iter = m_LogList.begin(); iter != m_LogList.end(); ++iter)
	{
		if (iter->m_iSeverity >= iHighSeverity || iter->m_iSeverity <= iLowSeverity)
		{
			OutLog.push_back(*iter);
		}
	}
	return OutLog;
}


void UI_Log::WriteToFile(const std::string& sFilename, bool bAppend, const std::string& sLogHeader) const
{return;
	std::ofstream File;
	if (bAppend)
	{
		File.open(std::string(sFilename).c_str(), std::ios::app);
	}
	else
	{
		File.open(std::string(sFilename).c_str(), std::ios::out | std::ios::trunc);
	}
	if (File.is_open())
	{
		File << sLogHeader << std::endl;
		for (t_LogList::const_iterator iter = m_LogList.begin(); iter != m_LogList.end(); ++iter)
		{
			//char sTime[100]; // hopefully the timestamp is no more than 100 characters, but it would be good to change this to a system define
			//mbstowcs(sTime, asctime(localtime(&iter->m_TimeStamp)), 100);
			time_t t;
			time(&t);
			std::string sTimeStamp(ctime(&t));
			File << sTimeStamp.substr(0, sTimeStamp.size() - 1) << " [ " << iter->m_iSeverity << " ] : " << iter->m_sMessage << std::endl;
		}
		File.close();
	}
}

}


